import { Chess } from "./Chess";

// 棋盘类
export class WuziqiBoard {
    size: number;
    // board: number[][];
    board: Chess[][]=[];
    currentPlayer: number;
    gameOver: boolean;
    lastMove: [number, number] | null;
    moveHistory: [number, number][];
    
    constructor(size: number = 16) {
        this.size = size;
        this.board.push([])
        for (let index = 1; index <= size; index++) {
            this.board.push([])
            this.board[index].push(null)
            for (let j = 1; j <= 16; j++) {
                let chess = new Chess()
                chess.status = 0;
                chess.xIndex = index
                chess.yIndex = j
                this.board[index].push(chess)
            }
        }
        // this.board = Array(size).fill(null).map(() => Array(size).fill(0));
        this.currentPlayer = 1; // 1: 玩家(黑棋), 2: AI(白棋)
        this.gameOver = false;
        this.lastMove = null;
        this.moveHistory = [];
    }
    
    // 在指定位置落子
    makeMove(row: number, col: number, player?: number,successCb?:Function): boolean {
        if (this.gameOver) {
            return false;
        }
        let chess = this.board[row][col] as Chess
        if(chess.status!=0){
            return false
        }
        
        const currentPlayer = player || this.currentPlayer;
        chess.status = currentPlayer;
        this.lastMove = [row, col];
        this.moveHistory.push([row, col]);
        successCb?.()
        // 检查是否获胜
        if (this.checkWin(row, col, currentPlayer)) {
            this.gameOver = true;
            return true;
        }
        
        // 切换玩家
        if (!player) {
            this.currentPlayer = this.currentPlayer === 1 ? 2 : 1;
        }else{
            this.currentPlayer=player===1? 2 : 1;
        }
        
        return true;
    }
    
    // 悔棋
    undoMove(cb:Function): boolean {
        if (this.moveHistory.length < 1) return false;
        
        const lastMove = this.moveHistory.pop()!;
        const [row, col] = lastMove;
        let chess = this.board[row][col]
        chess.status = 0
        this.currentPlayer = this.currentPlayer === 1 ? 2 : 1;
        this.gameOver = false;
        
        // 更新上一步
        this.lastMove = this.moveHistory.length > 0 ? 
            this.moveHistory[this.moveHistory.length - 1] : null;
        cb?.()
        return true;
    }
    
    // 检查是否获胜
    checkWin(row: number, col: number, player: number): boolean {
        const directions = [
            [0, 1],   // 水平
            [1, 0],   // 垂直
            [1, 1],   // 对角线
            [1, -1]   // 反对角线
        ];
        
        for (const [dx, dy] of directions) {
            let count = 1;  // 当前位置已经有一个棋子
            
            // 正向检查
            for (let i = 1; i <= 4; i++) {
                const newRow = row + dx * i;
                const newCol = col + dy * i;
                if (newRow >= 1 && newRow < this.size && 
                    newCol >= 1 && newCol < this.size && 
                    this.board[newRow][newCol].status === player) {
                    count++;
                } else {
                    break;
                }
            }
            
            // 反向检查
            for (let i = 1; i <= 4; i++) {
                const newRow = row - dx * i;
                const newCol = col - dy * i;
                if (newRow >= 1 && newRow < this.size && 
                    newCol >= 1 && newCol < this.size && 
                    this.board[newRow][newCol].status === player) {
                    count++;
                } else {
                    break;
                }
            }
            
            if (count >= 5) {
                return true;
            }
        }
        
        return false;
    }
    
    // 检查是否平局
    isDraw(): boolean {
        for (let row = 1; row <= this.size; row++) {
            for (let col = 1; col <= this.size; col++) {
                if (this.board[row][col].status === 0) {
                    return false;
                }
            }
        }
        return !this.gameOver;
    }
    
    // 克隆棋盘
    clone(): WuziqiBoard {
        const cloned = new WuziqiBoard(this.size);
        // cloned.board = this.board.map(row => [...row]);
        for (let i = 1; i <= 16; i++) {
            for (let j = 1; j <= 16; j++) {
                const element = cloned.board[i][j];
                element.status = this.board[i][j].status
                element.x = this.board[i][j].x
                element.y = this.board[i][j].y
                element.xIndex = this.board[i][j].xIndex
                element.yIndex = this.board[i][j].yIndex
            }
            
        }
        cloned.currentPlayer = this.currentPlayer;
        cloned.gameOver = this.gameOver;
        cloned.lastMove = this.lastMove ? [...this.lastMove] as [number, number] : null;
        cloned.moveHistory = [...this.moveHistory];
        return cloned;
    }
}